/**
 *
 */
package org.whuims.pois.dao;

import org.whuims.pois.action.result.ViewResult;
import org.whuims.pois.hibernate.BaseDao;
import org.whuims.pois.model.View;

import java.util.*;
import java.util.Map.Entry;

/**
 * @author herochen
 * @date 2010-4-19 @time 下午01:25:39
 */
public class ViewDao {
    BaseDao baseDao = new BaseDao();

    /**
     * 按照一定的分组进行观点的展现 <br>
     * group可以是人物，新闻标题，站点
     *
     * @param groupType
     * @return
     */
    public List<ViewResult> getViews(long topicId, String groupType) {

        List<ViewResult> result = new ArrayList<ViewResult>();
        if (!Arrays.asList(new String[]{"person", "newstitle", "newssite"})
                .contains(groupType.trim())) {
            groupType = "all";
        }
        List<View> list = getViews(topicId);
        if (list == null) {
            return result;
        }
        // 分组为all，就是请求全部
        if (groupType == "all") {
            ViewResult vr = new ViewResult();
            result = new ArrayList<ViewResult>();
            vr.setGroupName("all");
            vr.addViewList(getViews(topicId));
            result.add(vr);

        }// 按照新闻标题进行分组
        else if (groupType.equals("newssite") || groupType.equals("newstitle")
                || groupType.equals("person")) {
            result = getViewListByNewsInfo(groupType, list);
        }
        return result;

    }

    /**
     * 针对新闻标题和新闻站点和作者的分组
     *
     * @param groupType
     * @param list
     * @return
     */
    public List<ViewResult> getViewListByNewsInfo(String groupType, List<View> list) {
        List<ViewResult> result = new LinkedList<ViewResult>();
        Map<String, ViewResult> map = new HashMap<String, ViewResult>();
        for (int i = 0; i < list.size(); i++) {
            View v = list.get(i);
            String groupName;
            if(groupType.equals("person")){
                groupName = v.getcName();
            }else if(groupType.equals("newstitle")){
                groupName = v.getTitle();
            }else{
                groupName = v.getSite();
            }
            if (map.containsKey(groupName)) {
                ViewResult value = map.get(groupName);
                value.addView(v);
            } else {
                ViewResult vr = new ViewResult();
                vr.setGroupName(groupName);
                vr.setGroupType(groupType);
                vr.addView(v);
                map.put(groupName, vr);
            }
        }
        Iterator<Entry<String, ViewResult>> iterator = map.entrySet()
                .iterator();
        while (iterator.hasNext()) {
            Entry<String, ViewResult> entry = iterator.next();
            if (entry.getValue().getCount() <= 1)
                continue;
            result.add(entry.getValue());
        }
        Collections.sort(result, new Comparator<ViewResult>() {

            public int compare(ViewResult o1, ViewResult o2) {
                if (o1.getCount() < o2.getCount()) {
                    return 1;
                } else if (o1.getCount() > o2.getCount()) {
                    return -1;
                }
                return 0;
            }

        });
        return result;
    }

    /**
     * 得到一个view列表
     *
     * @param topicId
     * @return
     */
    public List<View> getViews(long topicId) {
        List<View> list1 = baseDao.find("from View where topicId = '"+topicId+"'");
        int total = list1.size();
        int startNo = total-100;
        List<View> list2 = baseDao.queryByParamArr(startNo, 100, "from View where topicId =?", topicId);
        return list2;
    }

}
